<?php
require_once dirname(__FILE__).'/ISms.php';
/**
 * Copyright (c) 2012, Jiehun.com.cn Inc. All Rights Reserved
 * @author huqingping@jiehun.com.cn
 * @date 2012-2-11
 * @version 1.0
 * @brief
 **/
final class MengwangSms extends ISms
{
	var $url;
	var $password;
	var $logFunc;
	var $port;
	var $user_name;
	var $curl;
	var $conf;
	var $time_out=10;
	var $connectionT = 10;

	function __construct($conf)
	{
		//平滑过度到新短信接口
		$this->url = $conf['server_url'];
		$this->user_name = $conf['user_name'];
		$this->password = $conf['password'];
		$this->port = $conf['port'];
		$this->curl = LibUtil::load('curl');
		$this->conf = $conf;
		if(!empty($conf['time_out']))
		{
			$this->time_out= $conf['time_out'];
		}
		if(!empty($conf['connect_time_out']))
		{
			$this->connectionT = $conf['connect_time_out'];
		}
	}

	/**
	 *发送短信接口
	 *@param string $msg 短信内容
	 *@param string or array $phone 要发送的手机号 如果手机号多个则是一维数组
	 */
	function send($msg,$phone)
	{
		if(empty($msg) || empty($phone))
		{
			return false;
		}

		if(mb_strlen($msg,'utf8')>350)
		{//短信长度不得大于350
			Logger::trace('send sms phoneCount is max :'.$phoneCount);
			return false;
		}

		$phoneCount = 1;
		if(is_array($phone))
		{
			$phone = array_unique($phone);
			$phoneCount = count($phone);
			if($phoneCount>=100)
			{//短信号码一次发送不得超过100个
				Logger::trace('send sms phoneCount is max :'.$phoneCount);
				return false;
			}
			$phone = implode(',',$phone);
		}

		$requestUrl = "{$this->url}/MongateCsSpSendSmsNew?userId={$this->user_name}&password={$this->password}&pszMobis={$phone}&pszMsg=".urlencode($msg)."&iMobiCount={$phoneCount}&pszSubPort={$this->port}";
		Logger::trace('send sms requesturl is '.$requestUrl);
		$requestResult = $this->getResult($requestUrl);
		Logger::trace('send sms response is '.var_export($requestResult,true));

		if($requestResult && !empty($requestResult[0]['value']) && strlen($requestResult[0]['value'])>10 && strlen($requestResult[0]['value'])<25)
		{
			Logger::trace('send sms suc');
			return $this->getUseCount($msg,63);
		}
		Logger::trace('send sms failed');
		return false;
	}

	/**
	*获取短信余额
	*/
	function getBalance()
	{
		$requestUrl = "{$this->url}/MongateQueryBalance?userId={$this->user_name}&password={$this->password}";
		$requestResult = $this->getResult($requestUrl);
		Logger::trace('sms getbalance  response is '.$requestResult[0]['value']);
		if($requestResult && isset($requestResult[0]['value']))
		{
			Logger::trace('sms balance is '.$requestResult[0]['value']);
			return $requestResult[0]['value'];
		}
		return false;
	}


	/**
	 *获取请求结果并转换xml
	 */
	function getResult($url)
	{
		$requestResult = $this->curl->get($url,array(CURLOPT_TIMEOUT=>$this->time_out,CURLOPT_CONNECTTIMEOUT=>$this->connectionT));
		if($requestResult && $requestResult['code']==200 && !empty($requestResult['content']))
		{
			$p = xml_parser_create();
			xml_parse_into_struct($p, $requestResult['content'], $vals, $index);
			xml_parser_free($p);
			return $vals;
		}
		return $requestResult;
	}
}
